<style lang="less" scoped>
</style>
<template>
    <base-model
        @model-close="modelClose"
        @model-ok="modelOk"
        title="新增物料"
        :value="value"
        :loading="loading"
        @input="updateshow">
        <Form :model="form" :label-width="120" :ref="ref" :rules="rule">
            <FormItem label="物料编号：" prop="nub">
                <base-input v-model="form.nub" placeholder="请输入物料编号"></base-input>
            </FormItem>
            <FormItem label="物料名称：" prop="name">
                <base-input v-model="form.name" placeholder="请输入物料名称"></base-input>
            </FormItem>
            <FormItem label="颜色：" prop="color">
                <base-input v-model="form.color" placeholder="请输入物料颜色"></base-input>
            </FormItem>
            <FormItem label="单位：" prop="unit">
                <base-input v-model="form.unit" placeholder="请输入单位"></base-input>
            </FormItem>
            <FormItem label="供应商：" prop="supplier">
                <base-input v-model="form.supplier" placeholder="请输入物料供应商"></base-input>
            </FormItem>
            <FormItem label="备注：" prop="remark">
                <base-input v-model="form.remark" placeholder="请输入备注"></base-input>
            </FormItem>
        </Form>
    </base-model>
</template>
<script>
    export default {
        name: 'addmateriel',
        props: {
            value: {
                type: Boolean,
                default: false
            }
        },
        data () {
            return {
                ref: 'addmateriel',
                form: {
                    nub: '',
                    name: '',
                    unit: '',
                    color: '',
                    supplier: '',
                    remark: ''
                },
                loading: true,
                rule: {
                    nub: this.rule.noNull('物料编号不能为空')
                },
                edit: false
            }
        },
        computed: {
        },
        methods: {
            setForm (form) {
                this.form = JSON.parse(JSON.stringify(form))
                this.edit = true
            },
            updateshow (show) {
                this.$emit('input', show)
            },
            reset () {
                this.$refs[this.ref].resetFields();
            },
            setdata (data) {
                this.$emit('setdata', data)
            },
            modelClose () {
                this.reset()
                this.form = {
                    nub: '',
                    name: '',
                    unit: '',
                    color: '',
                    supplier: '',
                    reamrk: ''
                }
                this.edit = false
                console.log(this.edit)
            },
            modelOk () {
                if (this.edit) {
                    this.$refs[this.ref].validate((valid) => {
                        if (valid) {
                            this.http.editMateriel(this, this.form)
                        }
                    })
                } else {
                    this.$refs[this.ref].validate((valid) => {
                        if (valid) {
                            this.http.addMateriel(this, this.form)
                        }
                    })
                }
                this.loading = false
                setTimeout(() => {
                    this.loading = true
                }, 100)
            }
        },
        components: {
        },
        mounted: function () {
        }
    };
</script>
